<?php
/*
 * @Author: your name
 * @Date: 2020-11-23 12:08:02
 * @LastEditTime: 2020-12-31 17:26:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/rule_log_service/RuleLogLinuxRuleService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter\rule_log_service;

use app\common\definition\InternetProtocolsDefinition;
use app\common\exception\CustomException;

class RuleLogLinuxRuleService extends RuleLogRuleService
{
    /**
     * @name: 设置处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 13:01:31
     */
    public function setProcess ()
    {
        preg_match('/\s+-j\s+(\w+)/i', $this->content, $math);

        if (empty($math))
        {
            throw new CustomException('未找到处理方式', 422);
        }
        if ($math[1] == 'accept')
        {
            $return = self::PROCESS_ALLOW;
        }
        else
        {
            $return = self::PROCESS_DROP;
        }

        return $return;
    }

    /**
     * @name: 设置方向
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:56:55
     */
    public function setDirection ()
    {
        preg_match('/-A\s+(\w+)/i', $this->content, $math);
        if (empty($math))
        {
           throw new CustomException('未找到流量方向', 422);
        }

        if ($math[1] == 'output')
        {
            $return = self::DIRECTION_OUTPUT;
        }
        else
        {
            $return = self::DIRECTION_INPUT;
        }
        
        return $return;
    }

    /**
     * @name: 设置源ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 13:04:28
     */
    public function setSrcIp ()
    {
        $math = [];

        $pattern_a = [
            '/\s+-s\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/32)?/i',
            '/\s+-m\s+iprange\s+--src-range\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i',
        ];

        foreach ($pattern_a as $pattern)
        {
            if(preg_match($pattern, $this->content, $math))
            {
                break;
            }
        }
        
        return $this->processIp($math);
    }

    /**
     * @name: 目标ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:57:40
     */
    public function setDestIp()
    {
        $math = [];

        $pattern_a = [
            '/\s+-d\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/32)?/i',
            '/\s+-m\s+ipranges\s+--dst-range\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i',
        ];

        foreach ($pattern_a as $pattern)
        {
            if(preg_match($pattern, $this->content, $math))
            {
                break;
            }
        }
        
        return $this->processIp($math);
    }

    /**
     * @name: 源端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:58:03
     */
    public function setSrcPort ()
    {
        preg_match('/\s+-m\s+multiport\s+--sports\s+(\d+([,:]\d+){0,13})/i', $this->content, $math);
        
        return $this->processPort($math);
    }

    /**
     * @name: 目标端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:58:03
     */
    public function setDestPort ()
    {
        preg_match('/\s+-m\s+multiport\s+--dports\s+(\d+([,:]\d+){0,13})/i', $this->content, $math);
        
        return $this->processPort($math);
    }

    /**
     * @name: 协议
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:58:03
     */
    public function setProtocols ()
    {
        preg_match('/\s+-p\s+(\w+)/i', $this->content, $math);

        if (empty($math))
        {
            return InternetProtocolsDefinition::ALL_PROTOCOL;
        }

        switch ($math[1])
        {
            case 'tcp' :
                $return = InternetProtocolsDefinition::TCP_PROTOCOL;
            break;
            case 'udp' :
                $return = InternetProtocolsDefinition::UDP_PROTOCOL;
            break;
            default :
                $return = InternetProtocolsDefinition::ALL_PROTOCOL;
        }

        return $return;
    }

    /**
     * @name: 设定数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-24 13:15:22
     */
    public function setData ()
    {
        $this->src_ip = $this->setSrcIp();
        $this->dest_ip = $this->setDestIp();
        $this->src_port = $this->setSrcPort();
        $this->dest_port = $this->setDestPort();
        $this->process = $this->setProcess();
        $this->protocols = $this->setProtocols();
        $this->direction = $this->setDirection();
    }
}